function dianZan() {
    var urlData = "GetPageViewCount:Jike-" + getPageName() + ",desktop";
    sendData(urlData);
}
function getPageName() {
    var fullUrl=decodeURI(window.location.href.substring(window.location.href.indexOf("jikepages") + 10));
    var lastDot=fullUrl.lastIndexOf('.');
    var urlValue =fullUrl.substring(0,lastDot);
    while (urlValue.indexOf("/") >= 0) {
        urlValue = urlValue.replace('/', '\\');
    }
    var srs = urlValue.split('\\');
    return srs[srs.length - 1];
}
function setHtmlStyle() {
    document.all[0].style.background = "#f5f5f5"
    document.body.style.width = "70%";
    document.body.style.margin = " 0 auto";
    document.body.style.background = "#efeeee";
    document.body.style.padding = "20px";
    document.body.style.borderRight = "1px solid #d1cccc";
    document.body.style.borderLeft = "1px solid #d1cccc";

    var div = document.createElement('h1')
    div.innerText = getPageName();
    document.body.insertBefore(div, document.body.firstChild);
}
function parseVideo() {
    var allADom = document.getElementsByTagName('a');
    for (var i = 0; i < allADom.length; i++) {
        if (allADom[i].href.endsWith('.mp4')) {
            var dom = document.createElement('div');
            dom.innerHTML = '<video width="600" controls="" name="media"><source src="' + allADom[i].href + '" type="video/mp4"></video>'
            var dr = document.createElement('br');
            allADom[i].parentNode.appendChild(dr);
            allADom[i].parentNode.appendChild(dom.firstChild);
        }
    }
}
function setDomStyle(dom, styles) {
    for (var name in styles) {
        dom.style[name] = styles[name];
    }
}
function loadComments() {
    //评论按钮2
    var commnetButton2 = document.createElement('button');
    commnetButton2.innerText = "\u8bc4\u8bba";
    document.body.appendChild(commnetButton2);

    //评论块
    var topDiv = document.createElement('div');
    topDiv.id = "topCommnentsDiv"
    setDomStyle(topDiv, {
        "border": "1px solid #a1a1a3",
        "padding": "10px",
        "background": "#f6f6f6"
    })
    document.body.appendChild(topDiv);

    //向下按钮
    var toTopButton = document.createElement('div');
    setDomStyle(toTopButton, {
        "position": 'fixed',
        "bottom": "100px",
        "right": "20px"
    })
    toTopButton.innerHTML = '<button type="button" style="padding:10px;margin-bottom:10px" onClick=window.scrollTo(0,0) title="\u56de\u9876\u90e8">\u25b2</button><br>' +
        '<button type="button" style="padding:10px;" onClick=window.scrollTo(0,document.body.scrollHeight) title="\u53bb\u8bc4\u8bba">\u25bc</button>';
    document.body.appendChild(toTopButton);

    var urlValue = decodeURI(window.location.href.substring(window.location.href.indexOf("jikepages") + 10).split(".")[0]);
    while (urlValue.indexOf("/") >= 0) {
        urlValue = urlValue.replace('/', '\\');
    }
    var srs = urlValue.split('\\');

    var commnetButton = document.createElement('button');
    commnetButton.innerText = "\u8bc4\u8bba";
    setDomStyle(commnetButton, { "marginLeft": "12px" });
    commnetButton2.onclick = commnetButton.onclick = function () {
        var usr = localStorage.getItem("AgCIM_Desktop_Doc.Usr");
        if (usr.length < 0 && usr.indexOf('@') < 0) {
            alert("\u8bf7\u4ece\u4e3b\u9875\u767b\u5f55");
            return;
        }
        window.scrollTo(0, document.body.scrollHeight)
        var usrName = usr.substring(0, usr.indexOf('@'));
        var str = prompt(usrName + ":\u8bf4\u51fa\u4f60\u7684\u610f\u89c1");
        if (str.length > 0) {
            sendData("SaveJikeComments:" + srs[srs.length - 1] + "," + usrName + "," + str);
            apppendComments(usrName, str, topDiv);
        }
    }
    var sibleDom = document.getElementById("reeditBtn")
    if (sibleDom) {
        sibleDom.parentNode.appendChild(commnetButton);
    }

    sendData("GetJikeComments:" + srs[srs.length - 1]);
    if (!onGetJikeCommentsHandler) {
        onGetJikeCommentsHandler = function (data) {
            var rows = data.split('#');
            topDiv.innerHTML = "";
            for (var i = 0; i < rows.length; i++) {
                if (rows[i].length < 2)
                    continue;
                var commontor = rows[i].substring(0, rows[i].indexOf(','));
                var commentText = rows[i].substring(rows[i].indexOf(',') + 1);;
                apppendComments(commontor, commentText, topDiv);
            }
        }
    }
}
function apppendComments(commontor, commentText, topDiv) {
    var div = document.createElement('div');
    setDomStyle(div, {
        "borderBottom": "1px dashed#d2d2de",
        "borderTop": "1px dashed#d2d2de",
        "paddingBottom": "10px"
    })

    var span0 = document.createElement('span');
    setDomStyle(span0, {
        "fontSize": "20px",
        "background": "url(https://pic1.zhimg.com/v2-abed1a8c04700ba7d72b45195223e0ff_l.jpg?source=06d4cd63)",
        "width": "40px",
        "height": "40px",
        "display": "block",
        "backgroundSize": "contain",
        "float": "left",
        "marginTop": "6px",
        "marginRight": "20px"
    })
    var span1 = document.createElement('span');
    span1.innerText = commontor;
    setDomStyle(span1, {
        "fontSize": "20px",
        "display": "table",
        "color": "black",
        "fontWeight": "bold"
    })

    var span2 = document.createElement('span');
    span2.innerText = commentText
    setDomStyle(span2, {
        "fontSize": "16px",
        "margin": "3px"
    })

    div.appendChild(span0)
    div.appendChild(span1)
    div.appendChild(span2)

    topDiv.appendChild(div);
}

function loadPageTagData() {
    var pageName = getPageName();

    var openFilterJikePage=function(e){
        window.open('http://121.33.231.74:4180/desktopDoc/jike/jikeMain.html?tag='+encodeURI(e.target.innerText.substring(1)));
    }
    var appendTagDiv = function (rows) {
        var sibleDom = document.getElementById("reeditBtn")
        if (!sibleDom) return;
        var existItems = document.getElementsByTagName('label');
        for (var i = existItems.length - 1; i >= 0; i--) {
            if (existItems[i].name == "pageTagData")
                existItems[i].parentNode.removeChild(existItems[i]);
        }
        for (var k = 0; k < rows.length; k++) {
            if(rows[k].length<1)continue;
            var label = document.createElement('label');
            label.name = "pageTagData";
            label.innerText = "#" + rows[k];
            label.onclick=openFilterJikePage;
            setDomStyle(label, {
                "background": "#6ec4e0",
                "marginLeft": "12px",
                "padding": "4px 10px",
                "color": "#ffffff",
                "cursor": "pointer",
                "borderRadius": "4px"
            })
            sibleDom.parentNode.appendChild(label);
        }
    }
   

    var tagButton = document.createElement('button');
    tagButton.innerText = "\u6807\u7b7e";
    setDomStyle(tagButton, { "marginLeft": "12px" });
    tagButton.onclick = function () {
        var str = prompt("\u521b\u5efa\u3001\u8865\u5145\u672c\u6587\u7684\u6807\u7b7e\uff0c\u53ef\u5b9e\u73b0\u8054\u52a8\u641c\u7d22\u3002\u7528\u9017\u53f7\u5206\u9694",
            window.currentTag ? window.currentTag : "");
        if (str.length > 0) {
            sendData("SaveJikeTagData:" + pageName + "," + str);
            appendTagDiv(str.split(','));
            window.currentTag = str;
        }
    }
    var sibleDom = document.getElementById("reeditBtn")
    if (sibleDom) {
        sibleDom.parentNode.appendChild(tagButton);
    }

    sendData("GetJikeTagData:" + pageName);
    if (!onGetJikeTagDatHandler) {
        onGetJikeTagDatHandler = function (data) {
            var rows = data.split('#');
            if (rows.length == 0) return;
            rows = rows[0].split(',');
            window.currentTag = rows;
            appendTagDiv(rows)
        }
    }
}
setHtmlStyle();
dianZan();
parseVideo();
loadComments();
loadPageTagData();